﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Hotel.WPF.HotelWCFSurIIS;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;

namespace Hotel.WPF.ui.Hotelss
{
    /// <summary>
    /// Interaction logic for W_Hotel.xaml
    /// </summary>
    public partial class W_Hotel : Window
    {
        HotelClient srvService;
        Hotels hotel;
        public W_Hotel()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            busyTool.Width = SystemParameters.WorkArea.Width;
            busyTool.Height = SystemParameters.WorkArea.Height;

            gridPrincipal.Width = SystemParameters.WorkArea.Width;
            gridPrincipal.Height = SystemParameters.WorkArea.Height-25;
            //srvService = new HotelClient();
            #region etablissement connexion avec serveur
            
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    srvService = new HotelClient();
                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        loadElementPage();
                                                                                        loadListeHotel();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            #endregion
            //loadElementPage();
            //loadListeRefCaractHotel();
        }

        private void loadElementPage()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    
                    //srvService = new HotelClient();
                    if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                    {
                        writeToBusyTool("Connection au serveur...");
                        srvService.Open();
                    }
                    writeToBusyTool("Chargement des pays...");
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        cb_pays.DataContext = srvService.getListPays();
                                        cb_pays.SelectedValuePath = "ID";
                                        cb_pays.DisplayMemberPath = "Nom_Pays";
                                    },
                                    null
                                    );

                    writeToBusyTool("Chargement des chaînes d'hotels...");
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        cb_chaineHotel.DataContext = srvService.getListChaineHotels();
                                        cb_chaineHotel.SelectedValuePath = "ID";
                                        cb_chaineHotel.DisplayMemberPath = "Nom_Chzine_Hotel";
                                    },
                                    null
                                    );

                    writeToBusyTool("Classement des hotels...");
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        cb_classement.DataContext = srvService.getListRefClassementEtoiles();
                                        cb_classement.SelectedValuePath = "ID";
                                        cb_classement.DisplayMemberPath = "code_Classement_etoile";
                                    },
                                    null
                                    );

                    writeToBusyTool("Caractéristiques des hotels...");
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        dg_refCaractHotel.DataContext = srvService.getListRefCaracteristiqueHotels();
                                    },
                                    null
                                    );

                    writeToBusyTool("Classement des chambres...");
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        dg_chambre.DataContext = srvService.getListChambreHotels();
                                    },
                                    null
                                    );

                    writeToBusyTool("Classement des types de chambre...");
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        cb_typeChamb.DataContext = srvService.getListRefTypeChambres();
                                        cb_typeChamb.SelectedValuePath = "ID";
                                        cb_typeChamb.DisplayMemberPath = "Nom_Type_Chambre";
                                    },
                                    null
                                    );

                    writeToBusyTool("Classement des statuts des chambres...");
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        cb_statutChamb.DataContext = srvService.getListRefStatutChambres();
                                        cb_statutChamb.SelectedValuePath = "ID";
                                        cb_statutChamb.DisplayMemberPath = "Nom_Statut_Chambre";
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        //srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }

        #region Hotels
        //ajout d'un hotel
        private void bt_ajouter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                hotel = new Hotels();
                if(cb_pays.SelectedValue != null)
                    hotel.pays_ID = Convert.ToInt32(cb_pays.SelectedValue);

                if(cb_classement.SelectedValue != null)
                    hotel.Classement_ID = Convert.ToInt32(cb_classement.SelectedValue);

                if(cb_classement.SelectedValue != null)
                    hotel.Chaine_Hotel_ID = Convert.ToInt32(cb_chaineHotel.SelectedValue);

                hotel.Nom_Hotel = tb_nomHotel.Text.Trim();
                hotel.Adresse_Hotel = tb_adresse.Text.Trim();
                hotel.Code_Postal_Hotel = tb_codePostal.Text.Trim();
                hotel.Ville_Hotel = tb_ville.Text.Trim();
                hotel.Telephone = tb_telephone.Text.Trim();
                hotel.Fax = tb_fax.Text.Trim();
                hotel.Url_Hotel = tb_siteweb.Text.Trim();

                if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                {
                    //writeToBusyTool("Connection au serveur...");
                    srvService.Open();
                }

                int resultatAjout = srvService.AddHotels(hotel);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("L'hotel [" + tb_nomHotel.Text + "] est bien ajouté",
                                    "Ajout Hotel",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    loadListeHotel();
                }
                else
                {
                    MessageBox.Show("Erreur d'ajout de l'hotel [" + tb_nomHotel.Text + "]",
                                    "Ajout Hotel",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                //srvService.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ajout Hotel", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //chargement de la liste des hotels
        private void loadListeHotel()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Chargement des hotels en cours...");
                    if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                    {
                        writeToBusyTool("Connection au serveur...");
                        srvService.Open();
                    }
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        var listeHotel = srvService.getListHotel();
                                        dg_hotel.DataContext = listeHotel;
                                        cb_hotelChamb.DataContext = listeHotel;
                                        cb_hotelChamb.SelectedValuePath = "ID";
                                        cb_hotelChamb.DisplayMemberPath = "Nom_Hotel";
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        //srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Hotel", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        #endregion

        #region Chambres        
        
        private void bt_ajoutChambre_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Chambre_Hotels chambre = new Chambre_Hotels();
                chambre.Hotel_ID = Convert.ToInt32(cb_hotelChamb.SelectedValue);
                chambre.Code_type_Chambre = Convert.ToInt32(cb_typeChamb.SelectedValue);
                chambre.Numero_Chambre = tb_numChambre.Text.Trim();
                chambre.Nombre_Etage_Chambre = Convert.ToInt32(tb_numEtageChamb.Text.Trim());
                chambre.Statut_ID = Convert.ToInt32(cb_statutChamb.SelectedValue);
                chambre.description_chambre = tb_descChamb.Text.Trim();
                chambre.Notes_Additionnelles = tb_noteAddiChamb.Text.Trim();                

                //srvService = new HotelClient();
                if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                {
                    //writeToBusyTool("Connection au serveur...");
                    srvService.Open();
                }
                int resultatAjout = srvService.AddChambreHotels(chambre);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("Enregistrement réussi",
                                    "Ajout Chambre",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    loadListeChambre();
                }
                else
                {
                    MessageBox.Show("Erreur d'enresgistrement",
                                    "Ajout Chambre",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                //srvService.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ajout Chambre", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //chargement de la liste des chambres
        private void loadListeChambre()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Chargement des chambre en cours...");
                    if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                    {
                        writeToBusyTool("Connection au serveur...");
                        srvService.Open();
                    }
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        var listeChambre = srvService.getListChambreHotels();
                                        dg_chambre.DataContext = listeChambre;
                                        //cb_hotelChamb.DataContext = listeHotel;
                                        //cb_hotelChamb.SelectedValuePath = "ID";
                                        //cb_hotelChamb.DisplayMemberPath = "Nom_Hotel";
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        //srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Chambre", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        #endregion

        private void image1_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }
    }
}
